package com.gfxy.modules.index.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gfxy.modules.index.mapper.SchoolIndexMapper;
import com.gfxy.modules.system.entity.SysDepart;
import com.gfxy.modules.system.service.ISysDepartService;
import com.gfxy.modules.system.service.ISysDictService;
import com.google.gson.JsonObject;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: lijixin 2021/9/12 6:01 下午
 */
@Service
public class SchoolIndexService {

    @Autowired
    private SchoolIndexMapper schoolIndexMapper;

    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private ISysDepartService sysDepartService;

    public Map<String,List> getTotalClasstimeByYear(String year){
        year = "2021";
        List<Map> selectResult = schoolIndexMapper.selectTotalClasstimeByYear(year);
        Map<String,List> result= new HashMap<>();
        List<String> orgList = new ArrayList<>();
        List<Double> valueList = new ArrayList<>();
        List<Double> averageList = new ArrayList<>();
        List<Double> teacherCountList = new ArrayList<>();

        for (Map map : selectResult) {
            System.out.println(map.keySet());
            orgList.add(map.get("depart_name").toString());
            valueList.add(Double.parseDouble(map.get("total").toString()));
            averageList.add(Double.parseDouble(map.get("average").toString()));
            teacherCountList.add(Double.parseDouble(map.get("teacherCount").toString()));
        }
        result.put("orgNames",orgList);
        result.put("totals",valueList);
        result.put("teacherCounts",teacherCountList);
        result.put("averages",averageList);

        return result;
    }

    public List<KeyValue> getClassTimeProportionByYear(String year){
        year = "2021";
        List<Map> selectResult = schoolIndexMapper.selectClassTimeProportionByYear(year);
        List<KeyValue> result = new ArrayList<>();
        if(selectResult.size() ==1){
            Map oneResult = selectResult.get(0);
            result.add(new KeyValue("理论课", oConvertUtils.isEmpty(oneResult.get("lecture"))? 0.0: Double.valueOf(oneResult.get("lecture").toString())));
            result.add(new KeyValue("课程设计",oConvertUtils.isEmpty(oneResult.get("courseDesign"))? 0.0:Double.valueOf(oneResult.get("courseDesign").toString())));
            result.add(new KeyValue("实践课",oConvertUtils.isEmpty(oneResult.get("practical"))? 0.0:Double.valueOf(oneResult.get("practical").toString())));
            result.add(new KeyValue("毕设指导",oConvertUtils.isEmpty(oneResult.get("graduateTutorial"))? 0.0:Double.valueOf(oneResult.get("graduateTutorial").toString())));
            result.add(new KeyValue("毕设答辩",oConvertUtils.isEmpty(oneResult.get("graduateDefense"))? 0.0:Double.valueOf(oneResult.get("graduateDefense").toString())));
            result.add(new KeyValue("考试",oConvertUtils.isEmpty(oneResult.get("examination"))? 0.0:Double.valueOf(oneResult.get("examination").toString())));
            result.add(new KeyValue("顶岗实习",oConvertUtils.isEmpty(oneResult.get("internship"))? 0.0:Double.valueOf(oneResult.get("internship").toString())));
            result.add(new KeyValue("实践讲课",oConvertUtils.isEmpty(oneResult.get("practicalTutorial"))? 0.0:Double.valueOf(oneResult.get("practicalTutorial").toString())));
        } else {
            result.add(new KeyValue("理论课", new Double(0.0)));
            result.add(new KeyValue("课程设计",new Double(0.0)));
            result.add(new KeyValue("实践课",new Double(0.0)));
            result.add(new KeyValue("毕设指导",new Double(0.0)));
            result.add(new KeyValue("毕设答辩",new Double(0.0)));
            result.add(new KeyValue("考试",new Double(0.0)));
            result.add(new KeyValue("顶岗实习",new Double(0.0)));
            result.add(new KeyValue("实践讲课",new Double(0.0)));
        }
        return result;
    }


    public static class KeyValue{
        private String name ;
        private Double value;

        public KeyValue(String name, Double value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Double getValue() {
            return value;
        }

        public void setValue(Double value) {
            this.value = value;
        }
    }


    /**
     * 查询教学建设默认视图
     * @param date
     */
    public JSONObject getTeachingDefaultView(String date){
        JSONObject result = new JSONObject();
        List<DictModel> dictList = sysDictService.queryDictItemsByCode("TEACHING_BUILDING_TYPE");
        Collections.sort(dictList, (dictModel1,dictModel2)->{
            return Integer.parseInt(dictModel1.getValue()) - Integer.parseInt(dictModel2.getValue());
        });
        List<String> itemNames = new ArrayList<>();
        Map<String,String> dictMap = new HashMap<>();
        for (DictModel dictModel : dictList) {
            itemNames.add(dictModel.getText());
            dictMap.put( dictModel.getText(),dictModel.getValue());
        }
        System.out.println(">>>>>>>>>>>>>>>");
        System.out.println(itemNames);

        List<SysDepart> deps = sysDepartService.queryDeptByPid("3");
        List<String> depNames = new ArrayList<>();
        for (SysDepart dep : deps) {
            depNames.add(dep.getDepartName());
        }

        List<Map> valueList = this.schoolIndexMapper.selectTeachingDefaultViewDate(date);
        Map<String, Map<String,String>> valueOrgMap = new HashMap<>();
        for (Map<String, String> map : valueList) {
            String orgName = map.get("depart_name");
            if(!valueOrgMap.containsKey(orgName)){
                valueOrgMap.put(orgName, new HashMap<String,String>());
            }

            Map<String,String> valueMap = valueOrgMap.get(orgName);
            valueMap.put(String.valueOf(map.get("project_type")), String.valueOf(map.get("score")));
        }

        System.out.println("valueOrgMap:" + valueOrgMap);

        Map<String,String> valueMap1 = valueOrgMap.get("计算机与软件学院");


        JSONArray valueJsonArray = new JSONArray();
        for (String itemName : itemNames) {
            JSONObject itemJsonObj = new JSONObject();

            itemJsonObj.put("name",itemName);
            itemJsonObj.put("type","bar");
            JSONObject emphasisObj = new JSONObject();
            emphasisObj.put("focus","series");
            itemJsonObj.put("emphasis", emphasisObj);
            JSONArray values = new JSONArray();
            for (String depName : depNames) {
                double v = 0.0;
                System.out.println("========"+ depName);
                if(valueOrgMap.containsKey(depName)){
                    Map<String,String> valueMap = valueOrgMap.get(depName);
                    String projectId = dictMap.get(itemName);
                    System.out.println(valueMap);

                    System.out.println(projectId);
                    System.out.println(valueMap.keySet());
                    System.out.println(valueMap.containsKey(projectId));
                    if(valueMap.containsKey(projectId)){
                        System.out.println(valueMap.get(projectId));
                        v = Double.parseDouble(valueMap.get(projectId));
                    }
                }
                values.add(v);
            }
            itemJsonObj.put("data",values);

            valueJsonArray.add(itemJsonObj);
        }



        result.put("items", itemNames);
        result.put("depNames",depNames);
        result.put("series",valueJsonArray);
        System.out.println(result);
        return result;
    }


}
